#include "MyString.h"
#include <cstring>

MyString::MyString() : size(0), capacity(15)
{
    string = new char[capacity];
}

MyString::MyString(const char *str)
{
    size = strlen(str);
    capacity = size + 1;
    string = new char[capacity];
    strcpy(string, str);
}

MyString::MyString(const MyString &s) : size(s.size), capacity(s.capacity)
{
    string = new char[capacity];
    strcpy(string, s.string);
}

MyString &MyString::operator=(const MyString &s)
{
    delete[] string; // 构造后进行赋值，所以可能已经有内存了，需要进行释放
    size = s.size;
    capacity = s.capacity;
    string = new char[capacity];
    strcpy(string, s.string);
    return *this;
}

MyString &MyString::operator+=(const MyString &s)
{
    size += s.size; // 新长度
    if (size + 1 > capacity)
    {
        capacity = size + 1;
        char *newS = new char[capacity];
        // 清空一下内存数据
        memset(newS, 0, capacity);
        strcpy(newS, string);
        delete[] string;
        string = newS;
    }
    strcat(string, s.string);
    return *this;
}

MyString &MyString::operator+=(const char &c)
{
    if (size + 1 >= capacity)
    {
        capacity *= 2;
        char *newS = new char[capacity];
        // 清空一下内存数据
        memset(newS, 0, capacity);
        strcpy(newS, string);
        delete[] string;
        string = newS;
    }
    string[size++] = c;
    string[size] = '\0';
    return *this;
}

MyString MyString::operator+(const MyString &s)
{
    MyString result = *this;
    result += s;
    return result;
}

MyString &MyString::operator-=(const MyString &s)
{
    char *dest = strstr(this->string, s.string);
    while (dest)
    {
        char *back = dest + s.size;
        while (*back != '\0')
        {
            *dest++ = *back++;
        }
        *dest = '\0';
        // 删完一个继续寻找下一个
        dest = strstr(this->string, s.string);
    }
    return *this;
}

MyString MyString::operator-(const MyString &s)
{
    MyString result = *this;
    result -= s;
    return result;
}

char &MyString::operator[](int index)
{
    return string[index];
}

bool MyString::operator==(const MyString &s)
{
    return strcmp(string, s.string) == 0;
}

MyString::operator int()
{
    return atoi(string);
}

MyString::operator double()
{
    return atof(string);
}

MyStringArray MyString::split(const MyString &delim)
{
    MyString temp = *this;
    MyStringArray result;
    char *dest = strtok(temp.string, delim.string);
    while (dest)
    {
        result += dest;
        dest = strtok(nullptr, delim.string);
    }
    return result;
}

void MyString::clear()
{
    memset(string, 0, capacity); // 先清空字符串
    size = 0;
}

MyString::~MyString()
{
    delete[] string;
}

std::ostream &operator<<(std::ostream &os, const MyString &s)
{
    std::cout << s.string;
    return os;
}

std::istream &operator>>(std::istream &is, MyString &s)
{
    s.clear();
    char c = 0;
    while ((c = getchar()) != '\n') // getchar(); //从终端读取一个字符
    {
        s += c;
    }
    return is;
}

MyStringArray::MyStringArray() : size(0), capacity(15)
{
    array = new MyString[capacity];

}

MyStringArray::MyStringArray(const MyStringArray &s)
{
    capacity = s.capacity;
    size = s.size;
    array = new MyString[capacity];
    for (int i = 0; i < size; i++)
    {
        array[i] = s.array[i];
    }
}

MyStringArray::~MyStringArray()
{
    delete[] array;
}

MyStringArray &MyStringArray::operator+=(const MyString &str)
{
    if (size >= capacity)
    {
        capacity *= 2;
        MyString *newA = new MyString[capacity];
        for (int i = 0; i < size; i++)
        {
            newA[i] = array[i];
        }
        delete[] array;
        array = newA;
    }
    array[size++] = str;
    return *this;
}

MyStringArray MyStringArray::operator+(const MyString &str)
{
    MyStringArray result = *this;
    result += str;
    return result;
}

void MyStringArray::show()
{
    for (int i = 0; i < size; i++)
    {
        std::cout << array[i] << std::endl;
    }
}

MyStringArray &MyStringArray::operator=(const MyStringArray &s)
{
    delete[] array;
    size = s.size;
    capacity = s.capacity;
    array = new MyString[capacity];
    for (int i = 0; i < size; i++)
    {
        array[i] = s.array[i];
    }
    return *this;
}

MyStringArray &MyStringArray::operator+=(const char &c)
{
    MyString a;
    if (a.size + 1 >= a.capacity)
    {
        capacity *= 2;
        char *newS = new char[a.capacity];
        // 清空一下内存数据
        memset(newS, 0, capacity);
        strcpy(newS, a.string);
        delete[] a.string;
        a.string = newS;
    }
    a.string[a.size++] = c;
    a.string[a.size] = '\0';
    array[size++] += a.string;
    return *this;
}


MyStringArray &MyStringArray::operator-=(const MyString &str)
{
    for (int i = 0; i < size; i++)
    {
        if (strcmp(array[i].string, str.string) == 0)
        {
            for (; i < size; i++)
            {
                array[i] = array[i + 1];
            }
            size--;
        }
    }
    return *this;
}

MyStringArray MyStringArray::operator-(const MyString &str)
{
    MyStringArray result = *this;
    result -= str;
    return result;
}

MyString& MyStringArray::operator[](int index)
{
    return array[index];
}



std::ostream &operator<<(std::ostream &os, MyStringArray &s)
{
    for(int i=0;i<s.size;i++)
    {
        std::cout<<s[i]<<std::endl;
    }
    return os;
}

std::istream &operator>>(std::istream &is, MyStringArray &s)
{
    MyString a;
    char c = 0;
    while ((c = getchar()) != '\n') // getchar(); //从终端读取一个字符
    {
        a += c;
    }
    s.array[s.size++] += a;
    return is;
}




